<?php

/**
 * Controller_resource
 * 教学资源
 */

class Controller_resource extends MyApp {
	/** 首页 **/
	public function __construct()
	{

		parent::__construct();

		$this->model->grade    = YOU::loadModel('grade');
		$this->model->class    = YOU::loadModel('class');
		$this->model->subject  = YOU::loadModel('subject');
		$this->model->staff    = YOU::loadModel('staff');
		$this->model->resource = YOU::loadModel('resource');
		$this->model->fav      = YOU::loadModel('fav');
		$this->model->dir      = YOU::loadModel('dir');
		$this->model->file     = YOU::loadModel('file');
		$this->model->baseset  = YOU::loadModel('baseset');
		$this->model->user     = YOU::loadModel('user');
		$this->model->log      = YOU::loadModel('log');
		
		

	}
	/** 首页 **/
	public function actionIndex()
	{

	}
	/** 档案管理展示页 **/
	public function actionList ()
	{	
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 左侧文件夹展开
		$class_lists = $this->model->class->getAll();
	
		foreach ($class_lists as $key => $value){

			$value['class_subject'] = unserialize($value['class_subject']);
			if (!empty($value['class_subject'])){

				$grade_data = $this->model->grade->getinfo($value['grade_id']);
				
				foreach ($value['class_subject'] as $k => $v){
					$grade[$grade_data['grade_id']]['grade_name'] = $grade_data['grade_name'];
					$grade[$grade_data['grade_id']]['subject'][$k] = $v;
					

				}
			}
		}
	
		// 权限
		$user 	= $this->user;
		// 进入模块的参数
		$grade_id		= $this->get->grade_id;
		$subject_id		= $this->get->subject_id;
		$part_id		= $this->get->part_id;
		// 查询条件
		$resource_title	= $this->get->resource_title;
		$from			= $this->get->from;
		$to				= $this->get->to;
		$staff_name		= $this->get->staff_name;
		$staff_names	= $this->get->staff_names;
		$subject_id		= $this->get->subject_id;
		$grade_id		= $this->get->grade_id;
		// 上传格式
		$file_type		= $this->get->file_type;

		$where = " WHERE 1=1 ";
		
		// 上传格式
		if(!empty($file_type)){
			// 视频
			if ($file_type =='mp4') {
				$where .= " AND file_type in ('mp4','bov','wmv','dvd','rmvb','avi','rm')  ";
			}elseif($file_type == 'qi'){
				// 其他
				$where .= " AND file_type in ('xlsx','docs','jpg','png','gif','zip','rar')  ";
			}else{
				// 单选
				$where .= " AND file_type ='{$file_type}' ";
			}
		}
		if(!empty($resource_title)){
			// 标题
			$where .= " AND resource_title LIKE '%{$resource_title}%' ";
			$this->tpl->assign("resource_title", $resource_title);
		}
		if(!empty($staff_names)){
			// 上传人(模糊搜索)
			$part_list = $this->model->staff->getNameMAll($staff_names);
            if (empty($part_list)) {
            	$part_list=0;
            }
			$where .= " AND user_id in ({$part_list}) ";
			$this->tpl->assign("staff_names", $staff_names);
		}
		if(!empty($from)){
			// 开始时间
			$where .= " AND create_time >= '{$from}' ";
			$this->tpl->assign("from", $from);
		}
		if(!empty($to)){
			// 结束时间
			$where .= " AND create_time <= '{$to}+1' ";
			$this->tpl->assign("to", $to);
		}
		if(!empty($staff_name)){
			// 点击名字查询
			$uid = $this->model->user->getIdByName($staff_name, "staff");
			$where .= " AND a.user_id = '{$uid}' ";

		}
		//	进入各班级、学科、分类
		if(!empty($part_id)){
			$where .= " AND part_id ='{$part_id}' ";
			$this->tpl->assign("part_id", $part_id);
		}
		if(!empty($grade_id)){
			$where .= " AND grade_id ='{$grade_id}' ";
			$this->tpl->assign("grade_id", $grade_id);
		}
		if(!empty($subject_id)){
			$where .= " AND subject_id ='{$subject_id}' ";
			$this->tpl->assign("subject_id", $subject_id);
		}
		// 全部分类
		$part_lists = $this->model->resource->getpartAll();
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_resource` {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$resource_lists = $this->db->fetchAll("SELECT * FROM `tbl_resource` {$where} ORDER BY resource_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach($resource_lists as $key=>$value){
			// 提交者
			$resource_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);
			// 时间
			$resource_lists[$key]['create_time'] = date('Y-m-d',strtotime($value['create_time']));
			// 分类名称
			$part_data = $this->model->resource->getpartInfo($value['part_id']);
			$resource_lists[$key]['part_name'] = $part_data['part_name'];
			// 取收藏
			$resource_lists[$key]['fav'] = $this->model->fav->getFav($value['resource_id'], $this->user['user_id']);
			// 上传文件大小
			$resource_lists[$key]['file_size'] = $this->model->file->formatBytes($value['file_size']);

		}
		
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));
		
		$this->tpl->assign("resource_lists", $resource_lists);
		$this->tpl->assign("part_lists", $part_lists);
		$this->tpl->assign("part_id", $part_id);
		$this->tpl->assign("collect_lists", $collect_lists);
		
		$this->tpl->assign("collect", $collect);
		$this->tpl->assign("grade_id", $grade_id);
		$this->tpl->assign("subject_id", $subject_id);
		$this->tpl->assign("grade", $grade);
		
		$this->tpl->display('resource/resource.list.tpl.php');

	}
	/** 教学资源添加 **/
	public function actionAdd()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);

		// 全部分类
		$part_lists = $this->model->resource->getpartAll();
		// 班级，学科id
		$grade_id          = $this->get->grade_id;
		$subject_id        = $this->get->subject_id;
		if (empty($grade_id)||empty($subject_id)) {
			Helper_Client::alert("请年级学科下添加");
		}
		if ($_POST){
			$resource_title         = $this->post->resource_title;
			$part_id         		= $this->post->part_id;
			$resource_content       = $this->post->resource_content;

			if (empty($resource_title)) {
				Helper_Client::location("请填写标题", "index.php?a=resource.add&grade_id={$grade_id}&subject_id={$subject_id}");
			}

			// 写入数据库
			$info = array(
			"resource_title"        => $resource_title,
			"part_id"         		=> $part_id,
			"resource_content"      => $resource_content,
			"grade_id"      		=> $grade_id,
			"subject_id"      		=> $subject_id,
			"user_id"         		=> $this->user['user_id'],
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_resource", $info);
			$resource_id = $this->db->insert_id();
			// 附件上传

			$file = $this->model->file->upload("resource_file", $resource_id, array("txt","ppt", "xls", "xlsx", "docs", "docx","doc", "pdf", "jpg", "png", "gif", "zip", "rar", "mp4", "AVI", "wma", "rmvb", "rm", "flash", "mid", "3gp"));

			if (!empty($file)){
				$info = array(
				"resource_file" => $file['fname'],
				"file_size"     => $file['fsize'],
				"file_type"     => $file['ftype'],
				);

				$this->db->do_update("tbl_resource", $info, "resource_id='{$resource_id}'");
			}

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=resource.list&grade_id={$grade_id}&subject_id={$subject_id} ");
		}

		$this->tpl->assign("part_lists", $part_lists);
		$this->tpl->display('resource/resource.add.tpl.php');
	}
	/** 教学资源修改 **/
	public function actionUpdate()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 全部分类
		$part_lists = $this->model->resource->getpartAll();

		$resource_id		= $this->get->resource_id;
		$grade_id			= $this->get->grade_id;
		$subject_id			= $this->get->subject_id;

		// 得到数据
		$resource_data = $this->model->resource->getInfo($resource_id);
		if ($resource_data['user_id'] != $this->user['user_id']){
			Helper_Client::alert("权限错误");
		}

		if (empty($resource_data)){
			Helper_Client::alert("当前资源不存在");
		}
		
		if ($_POST){

			$resource_title         = $this->post->resource_title;
			$part_id         		= $this->post->part_id;
			$resource_content       = $this->post->resource_content;

			if (empty($resource_title)) {
				Helper_Client::location("请填写标题", "index.php?a=resource.add&grade_id={$grade_id}&subject_id={$subject_id}");
			}

			// 写入数据库
			$info = array(
			"resource_title"        => $resource_title,
			"part_id"         		=> $part_id,
			"resource_content"      => $resource_content,
			"grade_id"      		=> $grade_id,
			"subject_id"      		=> $subject_id,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_update("tbl_resource", $info, "resource_id='{$resource_id}'");
			// 附件上传
			if ($this->post->resource_file_re=='1'){
				// 附件上传
				$file = $this->model->file->upload("resource_file", $resource_id, array("txt","ppt", "xls", "xlsx", "docs", "docx","doc", "pdf", "jpg", "png", "gif", "zip", "rar", "mp4", "AVI", "wma", "rmvb", "rm", "flash", "mid", "3gp"));
			
				if (!empty($file)){
					$info = array(
					"resource_file" => $file['fname'],
					"file_size"     => $file['fsize'],
					"file_type"     => $file['ftype'],
					);

					$this->db->do_update("tbl_resource", $info, "resource_id='{$resource_id}'");
				}
			}
			// 跳转
			Helper_Client::location("修改成功", "index.php?a=resource.list&grade_id={$grade_id}&subject_id={$subject_id} ");
		}

		$this->tpl->assign("part_lists", $part_lists);
		$this->tpl->assign("resource_data", $resource_data);
		$this->tpl->display('resource/resource.update.tpl.php');
	}
	/** 教学资源查看 **/
	public function actionDetails()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$resource_id		= $this->get->resource_id;
        $this->tpl->assign('resource_id',$resource_id);
        $uid = $this->user['user_id'];
        $this->tpl->assign('uid',$uid);
		$grade_id			= $this->get->grade_id;
		$subject_id			= $this->get->subject_id;
		// 得到数据
		$resource_data = $this->model->resource->getInfo($resource_id);
		// 提交者
		$resource_data['user_name'] = $this->model->user->getNameById($resource_data['user_id']);

		$this->tpl->assign("resource_data", $resource_data);
		$this->tpl->display('resource/resource.details.tpl.php');
	}
	/** 我的资源删除 **/
	public function actionDel()
	{
		$resource_id		= $this->get->resource_id;
		$grade_id			= $this->get->grade_id;
		$subject_id			= $this->get->subject_id;

		$this->db->query("DELETE  FROM `tbl_resource` WHERE resource_id='{$resource_id}' AND user_id='{$this->user['user_id']}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=resource.list&grade_id={$grade_id}&subject_id={$subject_id} ");
	}
	/** ---------------------------------------- 收藏 ---------------------------------- **/
	/** 我的资源收藏添加 **/
	public function actionfavAdd()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$user_id =$this->user['user_id'];

		$resource_id		= $this->get->resource_id;
		$grade_id			= $this->get->grade_id;
		$subject_id			= $this->get->subject_id;
		// 得到数据
		$resource_data = $this->model->resource->getInfo($resource_id);

		if (empty($resource_data)){
			Helper_Client::alert("当前资源不存在");
		}

		// 查找收藏表是是否已经收藏过，
		$resource_data = $this->model->fav->getFav($resource_id,$user_id);

		if (empty($resource_data) == 0){
			Helper_Client::alert("当前资源已经收藏");
		}else{
			// 写入数据库
			$info = array(
			"resource_id"        	=> $resource_id,
			"grade_id"        		=> $grade_id,
			"subject_id"        	=> $subject_id,
			"user_id"         		=> $user_id,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_fav", $info);
			// 跳转
			Helper_Client::location("收藏成功", "index.php?a=resource.favlist ");
		}
	}
	/** 我的资源收藏删除 **/
	public function actionFavDel()
	{
		$fav_id				= $this->get->fav_id;
		$grade_id			= $this->get->grade_id;
		$subject_id			= $this->get->subject_id;

		$this->db->query("DELETE  FROM `tbl_fav` WHERE fav_id='{$fav_id}' AND user_id='{$this->user['user_id']}'");

		// 跳转
		Helper_Client::location("取消收藏成功", "index.php?a=resource.favlist&grade_id={$grade_id}&subject_id={$subject_id} ");

	}
	/** 我的收藏列表 **/
	public function actionFavList ()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 左侧文件夹展开
		$class_lists = $this->model->class->getAll();
	
		foreach ($class_lists as $key => $value){

			$value['class_subject'] = unserialize($value['class_subject']);
			if (!empty($value['class_subject'])){

				$grade_data = $this->model->grade->getinfo($value['grade_id']);
				
				foreach ($value['class_subject'] as $k => $v){
					$grade[$grade_data['grade_id']]['grade_name'] = $grade_data['grade_name'];
					$grade[$grade_data['grade_id']]['subject'][$k] = $v;
					

				}
			}
		}
		// 进入模块的参数
		$grade_id		= $this->get->grade_id;
		$subject_id		= $this->get->subject_id;
		$part_id		= $this->get->part_id;
		// 查询条件
		$resource_title	= $this->get->resource_title;
		$from			= $this->get->from;
		$to				= $this->get->to;
		$staff_name		= $this->get->staff_name;
		$staff_names	= $this->get->staff_names;
		$subject_id		= $this->get->subject_id;
		$grade_id		= $this->get->grade_id;
		// 上传格式
		$file_type		= $this->get->file_type;

		$where = "";

		// 上传格式
		if(!empty($file_type)){
			// 视频
			if ($file_type =='mp4') {
				$where .= " AND a.file_type in ('mp4','bov','wmv','dvd','rmvb','avi','rm')  ";
			}elseif($file_type == 'qi'){
				// 其他
				$where .= " AND a.file_type in ('xlsx','docs','jpg','png','gif','zip','rar')  ";
			}else{
				// 单选
				$where .= " AND a.file_type ='{$file_type}' ";
			}
		}
		if(!empty($resource_title)){
			// 标题
			$where .= " AND resource_title LIKE '%{$resource_title}%' ";
			$this->tpl->assign("resource_title", $resource_title);
		}
		if(!empty($staff_names)){
			// 上传人(模糊搜索)
			$part_list = $this->model->staff->getNameMAll($staff_names);
			if (empty($part_list)) {
				$part_list['staff_id']=0;
			}
			
			$where .= " AND a.user_id = {$part_list['staff_id']} ";
			$this->tpl->assign("staff_names", $staff_names);
		}
		if(!empty($from)){
			// 开始时间
			$where .= " AND a.create_time >= '{$from}' ";
			$this->tpl->assign("from", $from);
		}
		if(!empty($to)){
			// 结束时间
			$where .= " AND a.create_time <= '{$to}+1' ";
			$this->tpl->assign("to", $to);
		}
		if(!empty($staff_name)){
			// 点击名字查询
			$uid = $this->model->user->getIdByName($staff_name, "staff");	
			$where .= " AND a.user_id = '{$uid}' ";
			
		}
		//	进入各班级、学科、分类
		if(!empty($part_id)){
			$where .= " AND a.part_id ='{$part_id}' ";
			$this->tpl->assign("part_id", $part_id);
		}
		if(!empty($grade_id)){
			$where .= " AND a.grade_id ='{$grade_id}' ";
			$this->tpl->assign("grade_id", $grade_id);
		}
		if(!empty($subject_id)){
			$where .= " AND a.subject_id ='{$subject_id}' ";
			$this->tpl->assign("subject_id", $subject_id);
		}
		
		// 全部分类
		$part_lists = $this->model->resource->getpartAll();
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_resource` AS a,`tbl_fav` AS b WHERE a.resource_id = b.resource_id AND b.user_id ='{$this->user['user_id']}' {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$resource_lists = $this->db->fetchAll("SELECT a.* FROM `tbl_resource` AS a,`tbl_fav` AS b WHERE a.resource_id=b.resource_id AND b.user_id ='{$this->user['user_id']}' {$where} ORDER BY b.fav_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		foreach($resource_lists as $key=>$value){
			// 上传人名称
			$resource_lists[$key]['staff_name'] = $this->model->user->getNameById($value['user_id']);

			// 时间
			$resource_lists[$key]['create_time'] = date('Y-m-d',strtotime($value['create_time']));
			// 分类名称
			$part_data = $this->model->resource->getpartInfo($value['part_id']);
			$resource_lists[$key]['part_name'] = $part_data['part_name'];
			// 获得收藏
			$resource_lists[$key]['fav'] = $this->model->fav->getFav( $value['resource_id'],$value['user_id']);
			// 上传文件大小
			$resource_lists[$key]['file_size'] = $this->model->file->formatBytes($value['file_size']);

		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("resource_lists", $resource_lists);
		$this->tpl->assign("part_lists", $part_lists);
		$this->tpl->assign("part_id", $part_id);
		$this->tpl->assign("collect_lists", $collect_lists);

		$this->tpl->assign("collect", $collect);
		$this->tpl->assign("grade_id", $grade_id);
		$this->tpl->assign("subject_id", $subject_id);
		$this->tpl->assign("grade", $grade);
		$this->tpl->display('resource/resource.fav.list.tpl.php');

	}
	/** ----------------------------- 教学资源分类----------------------------------- **/
	/** 教学资源分类列表 **/
	public function actionClassifyList ()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_resource_part`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$part_lists = $this->db->fetchAll("SELECT * FROM `tbl_resource_part` ORDER BY part_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("part_lists", $part_lists);
		$this->tpl->display('resource/classify.list.tpl.php');
	}
	/** 教学资源分类添加 **/
	public function actionClassifyAdd ()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);

		if ($_POST){

			$part_name    	    = $this->post->part_name;

			if (empty($part_name)) {
				Helper_Client::location("请填写分类名称", "index.php?a=resource.classifylist");
			}

			// 写入数据库
			$info = array(

			"part_name"             =>$part_name,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_resource_part", $info);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=resource.classifylist");
		}
		$this->tpl->display('resource/classify.add.tpl.php');
	}
	/** 教学资源分类修改 **/
	public function actionClassifyUpdate ()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$part_id				= $this->get->part_id;
		// 获取学生
		$part_data = $this->db->fetchRow("SELECT * FROM `tbl_resource_part` WHERE part_id = '{$part_id}'");
		
		if (empty($part_data)){
			Helper_Client::alert("当前分类不存在");
		}

		if ($_POST){

			$part_name			= $this->post->part_name;

			// 写入数据库
			$info = array(

			"part_name"  			=> $part_name,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_update("tbl_resource_part", $info, "part_id='{$part_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=resource.classifylist");
		}

		$this->tpl->assign("part_data", $part_data);
		$this->tpl->display('resource/classify.update.tpl.php');
	}
	/** 教学资源分类删除 **/
	public function actionClassifyDel()
	{
		$part_id		  = $this->get->part_id;

		$this->db->query("DELETE  FROM `tbl_resource_part` WHERE part_id='{$part_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=resource.classifylist");
	}
	/** 下载**/
	public function actionoutput(){
		$fname = $this->get->resource_file;

		$fpath = UPLOAD_PATH."/resource_file/{$fname}";

		// 输出下载文件头
		header("Content-Disposition: attachment; filename={$fname}");
		// 在线输出
		readfile($fpath);

	}
	public function actionqa(){
        if($_POST){
            $uid = $this->user['user_id'];
            $uname = $this->model->user->getNameById($uid);
            $rid = $this->post->resource_id;
            $question = $this->post->question;
            $row = $this->db->fetchRow("select * from `tbl_resource` WHERE resource_id={$rid}");
            $row['qa'] .= " {$uname} : $question ";
            $info=array(
                "qa"    =>  $row['qa'],
            );
            $this->db->do_update("tbl_resource", $info, "resource_id='{$rid}'");
            Helper_Client::location("发送成功", "index.php?a=resource.list");
        }

    }
}
?>